/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.model.viewadpater;

import java.util.HashMap;
import java.util.List;

import org.niclab.vanbus.R;
import org.niclab.vanbus.model.BusRoute;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class BusRouteExpandableListAdapter extends BaseExpandableListAdapter{
	
	private Context context;
	private int groupLayout;
	private int childLayout;
	private List<BusRoute> groupData;
	private HashMap<String, List<BusRoute>> childData;
	
	public BusRouteExpandableListAdapter(Context ctx,int groupLayout, int childLayout, List<BusRoute> routes){
		super();
		this.context = ctx;
		this.groupLayout =groupLayout;
		this.childLayout = childLayout;
		this.groupData = routes;
		childData = new HashMap<String,List<BusRoute>>();
	
		
		
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		
		String routeId= groupData.get(groupPosition).getRouteId();
		List<BusRoute> routes = childData.get(routeId);
		if (routes!=null)
			return routes.get(childPosition);
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View curView = convertView;
		
		//this is not an update
		if(curView==null){
			/*LayoutInflater vi = (LayoutInflater) parent.getContext()
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);*/
			curView=LayoutInflater.from(this.context).inflate(childLayout, parent,false);
			//curView = vi.inflate(childLayout,parent,false);
		}
		
		BusRoute route =(BusRoute) getChild(groupPosition,childPosition);
		
		TextView routeDir = (TextView)curView.findViewById(R.id.busroute_dir_tv);
		routeDir.setText(route.getRouteDir());
		
		TextView routeDirDesc = (TextView)curView.findViewById(R.id.busroute_dir_desc_tv);
		String desp = route.getRouteDirDesp();
		routeDirDesc.setText(desp);
		
		return curView;
		
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		String routeId= groupData.get(groupPosition).getRouteId();
		List<BusRoute> routes = childData.get(routeId);
		if(routes==null)
			return -1;
		else
			return routes.size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupData.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groupData.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		
		View curView = convertView;
		
		//this is not an update
		if(curView==null){
			/*LayoutInflater vi = (LayoutInflater) parent.getContext()
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);*/
			curView=LayoutInflater.from(this.context).inflate(groupLayout, parent,false);
			//curView = vi.inflate(groupLayout,parent,false);
		}
		
		BusRoute route =(BusRoute) getGroup(groupPosition);
		
		TextView routeId = (TextView)curView.findViewById(R.id.busroute_id);
		routeId.setText(route.getRouteId());
		
		TextView routeName = (TextView)curView.findViewById(R.id.busroute_name);
		String name = route.getRouteName();
				
		routeName.setText(name.substring(name.indexOf('-')+1));
		
		
		return curView;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		
		return true;
	}
	
	
	public void populateChildren(int groupPosition, List routes){
		BusRoute bus = groupData.get(groupPosition);
		
		childData.put(bus.getRouteId(), routes);
		
	}

}
